<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * Route Controller
 * @author Simon, Denis, Dennis
 *
 */
class Route extends CI_Controller {

	/**
	 * 
	 * Constructor
	 */
	public function __construct()
	{
		parent::__construct();
		require_once('user.php');
		$this->user=new User();

		// 		$this->load->model('routes_model');
	}
	/**
	 * 
	 * index Function
	 */
	public function index()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'route';
		$this->load->view('menu',$menu);
		if($loggedIn==true) {
			$this->load->view('route');
		} else {
			$this->load->view('503');
		}
		$this->load->view('footer');

	}
	/**
	 * 
	 * is used to Display the Page for a new Route
	 */
	public function newRoute()
	{

		$this->load->view('header');

		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'route';
		$this->load->view('menu',$menu);
		$this->load->view('route_newroute');
		$this->load->view('footer');



		$ownerID=$this->user->getID();
		
	}
	/**
	 * 
	 * Stores a New Route in the DB
	 */
	public function addNewRoute() {
		$ownerID=$this->user->getID();
		$name = trim($_POST['name']);
		$routeFile = trim($_POST['routeFile']);
		$difficulty = trim($_POST['difficulty']);
		$ground = trim($_POST['ground']);
		$length = trim($_POST['length']);
		$public = 0;
		$favorite = trim($_POST['favorite']);
		if($favorite=="false") $favorite=0;
		else $favorite=1;
		
		
			
		$this->routes_model->addRoute($ownerID,$name,$routeFile,$difficulty,$ground,$length,$public,$favorite);
	}
	/**
	 * 
	 * is used to Display the Page for edit Route
	 */
	public function editRoute()
	{
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'route';
		$this->load->view('menu',$menu);


		$data['routes'] = $this->routes_model->getRouteFromUser($_SESSION['id']);

		$this->load->view('route_editroute', $data);
		$this->load->view('footer');
	}

	/**
	 * 
	 * Function to load a Route File of a route
	 * @param  $route_id ID of the Route
	 */
	public function loadRouteFile($route_id)
	{
		$route =$this->routes_model->getRouteByID($route_id);
		echo $route->RouteFile;

	}


	/**
	 * 
	 * Function to save a existing route, only difficulty, ground and favorite can be changed
	 */
	public function saveRoute()
	{
		$data['routes'] = $this->routes_model->getRouteFromUser($_SESSION['id']);
		
		$routeID = trim($_POST['routeID']);
		$difficulty = trim($_POST['difficulty']);
		$ground = trim($_POST['ground']);
		$public = 0;
		$favorite = trim($_POST['favorite']);

		if($favorite=="false") $favorite=0;
		else $favorite=1;
		
		$this->routes_model->saveRoute($routeID,$difficulty,$ground,$public,$favorite);

	}
	/**
	 * 
	 * function to delete a existing route
	 */
	public function deleteRoute(){
		$routeID = trim($_POST['routeID']);
		$error = $this->routes_model->delRoute($routeID);
		if($error != 1){
			return $error;
		}
	}
}